const config = require("../../myconfig.js").config;
const register=require("./register").register
function login(code) {  
  var token=""
  var session=""
  token=wx.getStorageSync('token')
  session=wx.getStorageSync("session")
  console.log("login获得权限",token,session)
  if (token==""||session==""){
    console.log("新用户注册")
    register(code); 
    return
  }
  console.log("发送login请求")
  wx.request({  
    url: config.serverApi + "login",  
    method: "POST",  
    data: {  
      "token": token,  
      "session": session 
    },  
    success: res => {  
      if (res.statusCode === 200) {  
        console.log("登录成功",res);
        wx.setStorage({key:"session",data:res.data.session})
      } else {  
        // 登录失败，尝试注册  
        console.log("登录失败",res)
        this.register(code);  
      }  
    },  
    fail: err => {  
      console.log("登录请求失败", err);  
      // 登录请求失败，尝试注册  
      this.register(code);  
    }  
  });  
}
module.exports={
  login
}